/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Threads;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author QuangHau
 */
public class updateImage extends Thread{
    JLabel lbImage;
    int maTinTuc;
    String url;
    public updateImage(JLabel _lbImage, int _maTinTuc, String _url) {
        lbImage = _lbImage;
        maTinTuc = _maTinTuc;
        url = _url;
    }
    
    @Override
    public void run() {
        File f = new File("Image\\" + maTinTuc + ".jpg");
        if (f.exists()) {
            try {
                BufferedImage bImageFromConvert = ImageIO.read(f);
                showImage(lbImage, bImageFromConvert);
            } catch (IOException ex) {
                
            }
        } else {
            try {
                BufferedImage bImageFromConvert = ImageIO.read(new URL(url));
                showImage(lbImage, bImageFromConvert);
            } catch (IOException e) {
            }
        }
    }
    
     public static void showImage (JLabel lable, BufferedImage image){
            int x = 106;
            int y = 91;
            int ix = image.getWidth();
            int iy = image.getHeight();
            int dx = x;
            int dy = dx * iy / ix;
            if(dx > x || dy > y ){
                dy = y;
                dx = dy * ix / iy;
            }
            
            ImageIcon icon = new ImageIcon(image.getScaledInstance(dx, dy, BufferedImage.SCALE_SMOOTH));
            lable.setIcon(icon);
            lable.setText("");
    }
}
